import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import { getSearchByCode, getCodeList } from '../../../../services/allposition_service';

export default {
  namespace: 'report',
  state: {
    stocks: { code: 0, message: '', list: [] },
    stocksFilters: {},
  },

  effects: {
    *GetCodeList({ payload, callback }, { call, put }) {
      const response = yield call(getCodeList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveStocks',
          payload: {
            list: { code: 1, message: response.message, list: response.data },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetSearchByCode({ payload, callback }, { call, put }) {
      const response = yield call(getSearchByCode, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },
  },
};
